/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lansupportserver;

import ObjectHandler.LanSupportMessage;
import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Quan
 */
public class FrameChat extends javax.swing.JFrame {

    private String conversion = "";
    private Socket sendSocket;

    public FrameChat(Socket sendSocket) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.sendSocket = sendSocket;
    }

    public void AddMessage(String message) {
        if (!"".equals(message)) {
            if (!"".equals(conversion)) {
                conversion += "\r\n";
            }
            conversion += "Clien at " + sendSocket.getInetAddress() + ": " + message;
            jtf_1.setText(conversion);
        }
    }

    private void SendMesage() {
        String input = jtf_2.getText();
        if (!"".equals(input)) {
            jtf_2.setText(null);
            if (!"".equals(conversion)) {
                conversion += "\n\r";
            }
            conversion += input;
            jtf_1.setText(conversion);
            LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.ChatMessageType);
            byte[] data = null;
            try {
                data = input.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(FrameChat.class.getName()).log(Level.SEVERE, null, ex);
            }
            lanSupportMessage.setData(data);
            lanSupportMessage.Send(sendSocket);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtf_1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtf_2 = new javax.swing.JTextArea();

        setTitle("Chat - Lan Support Server");

        jtf_1.setColumns(20);
        jtf_1.setEditable(false);
        jtf_1.setRows(5);
        jScrollPane1.setViewportView(jtf_1);

        jButton1.setText("Gửi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jtf_2.setColumns(20);
        jtf_2.setRows(5);
        jtf_2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtf_2KeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jtf_2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.SendMesage();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtf_2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_2KeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            this.SendMesage();
            evt.consume();
        }
    }//GEN-LAST:event_jtf_2KeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jtf_1;
    private javax.swing.JTextArea jtf_2;
    // End of variables declaration//GEN-END:variables
}
